layui.define(['jquery', 'conf', 'func', 'layer'], function (exports) {
    const {jquery: $, conf, func, layer} = layui;
    exports('view', {
        render: function (route) {
            return new Promise(function (resolve, reject) {
                if (route.indexOf("?") !== -1) {
                    route = route.substr(0, route.indexOf("?"));
                }
                $.ajax({
                    url: func.getRootPath() + "/view" + route + '.html' + '?random=' + Math.random(),
                    type: 'get',
                    dataType: 'html',
                    success: function (html) {
                        //渲染内容到页面中
                        $('body').append(html);

                        func.receiveMessage('render_success', function(){
                            $("body > #preLoading").remove();

                            resolve();

                        });
                    },
                    error: function (xhr) {
                        //重定向到错误通用提示页面
                        location.href = func.url("/error", {
                            code: xhr.status,
                            error: encodeURI(xhr.statusText),
                        });
                        location.reload();
                    }
                })
            });

        },
        /**
         * 页面跳转
         * @param route
         * @param data
         * @return Promise
         */
        route: function (route, data = {}) {
            if (route === "/") {
                route = conf.view.layout;
            }
            location.hash = route + "?" + $.param(data);
            //刷新页面
            location.reload();
        },
        newTab:function(title, route, data){
            if (route === "/") {
                route = conf.view.layout;
            }
            parent.layout.openTab(title, route + "?" + $.param(data));
        }
    });
});
